package sd.zhr.moodcapsule.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import sd.zhr.moodcapsule.common.WrapMapper;
import sd.zhr.moodcapsule.common.Wrapper;
import sd.zhr.moodcapsule.pojo.MoodCapsule;
import sd.zhr.moodcapsule.service.MoodCapsuleServiceImpl;

import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/moodcapsule") // 指定基础路径
@CrossOrigin // 允许跨域请求
public class MoodCapsuleController {

    @Autowired
    private MoodCapsuleServiceImpl moodCapsuleService;

    /**
     * 获取所有 MoodCapsule 记录。
     *
     * @return 包含所有 MoodCapsule 记录的包装响应
     */
    @GetMapping("/getAll")
    @Secured({"ROLE_ADMIN", "ROLE_COMMON"}) // 限制访问角色
    public Wrapper<List<MoodCapsule>> getAllMoodCapsules(MoodCapsule moodCapsule) {
        List<MoodCapsule> moodCapsuleList = moodCapsuleService.getAllMoodCapsules(moodCapsule);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, moodCapsuleList);
    }

    /**
     * 根据主键获取一条 MoodCapsule 记录。
     *
     * @param id 主键ID
     * @return 包含 MoodCapsule 记录的包装响应
     */
    @GetMapping("/getById/{id}")
    @Secured({"ROLE_ADMIN"})
    public Wrapper<MoodCapsule> getMoodCapsuleById(@PathVariable Integer id) {
        MoodCapsule moodCapsule = moodCapsuleService.selectByPrimaryKey(id);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, moodCapsule);
    }

    /**
     * 插入一条 MoodCapsule 记录。
     *
     * @param moodCapsule 要插入的记录对象
     * @return 包含插入结果的包装响应
     */
    @PostMapping("/save")
    @Secured({"ROLE_ADMIN"})
    public Wrapper<Boolean> saveMoodCapsule(@RequestBody MoodCapsule moodCapsule) {
        System.out.println(moodCapsule);
        int result = moodCapsuleService.insert(moodCapsule);
        boolean isSuccess = result > 0;
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, isSuccess);
    }

    /**
     * 更新一条 MoodCapsule 记录。
     *
     * @param moodCapsule 要更新的记录对象
     * @return 包含更新结果的包装响应
     */
    @PutMapping("/update")
    @Secured({"ROLE_ADMIN"})
    public Wrapper<Boolean> updateMoodCapsule(@RequestBody MoodCapsule moodCapsule) {
        int result = moodCapsuleService.updateByPrimaryKeySelective(moodCapsule);
        boolean isSuccess = result > 0;
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, isSuccess);
    }

    /**
     * 删除一条 MoodCapsule 记录。
     *
     * @param id 要删除的记录的主键ID
     * @return 包含删除结果的包装响应
     */
    @DeleteMapping("/delete/{id}")
    @Secured({"ROLE_ADMIN"})
    public Wrapper<Boolean> deleteMoodCapsule(@PathVariable Integer id) {
        int result = moodCapsuleService.deleteByPrimaryKey(id);
        boolean isSuccess = result > 0;
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, isSuccess);
    }
}
